package br.inatel.wiiproject.controller;
//import java.rmi.Remote;
//import java.util.ArrayList;
import java.awt.*;
import javax.swing.*;

import br.inatel.wiiproject.data.Coordinate;
import br.inatel.wiiproject.data.StoredData;
import br.inatel.wiiproject.view.MainFrame;
import br.inatel.wiiproject.view.RemotePanel;
import wiiremotej.*;
import wiiremotej.event.*;
//import javax.sound.sampled.*;
//import java.io.*;

/**
 * Implements WiiRemoteListener and acts as a general test class. Note that you can ignore the main method pretty much, as it mostly has to do with the graphs and GUIs.
 * At the very end though, there's an example of how to connect to a remote and how to prebuffer audio files.
 * @author Michael Diamond
 * @version 1/05/07
 */

public class WRLImpl extends WiiRemoteAdapter
{
	
    private static boolean accelerometerSource = true; //true = wii remote, false = nunchuk
    
    private WiiRemote remote;

    private static int remoteX = 0;
    private static int remoteY = 0;
    private static int nunchukX = 0;
    private static int nunchukY = 0;
    
    private RemotePanel remotePanel = null;
    private RemotePanel nunchukPanel = null;
    
    public WRLImpl(WiiRemote remote, boolean isFirstRemote)
    {
        this.remote = remote;
        if (isFirstRemote){
        	remotePanel = MainFrame.getInstance().getRightRemotePanel();
        	nunchukPanel = MainFrame.getInstance().getRightNunchukPanel();
        }
        else {
        	remotePanel = MainFrame.getInstance().getLeftRemotePanel();
        	nunchukPanel = MainFrame.getInstance().getLeftNunchukPanel();
        }
    }
    
    public void disconnected()
    {
        System.out.println("Remote disconnected... Please Wii again.");
        System.exit(0);
    }
    
    public void statusReported(WRStatusEvent evt)
    {
        System.out.println("Battery level: " + (double)evt.getBatteryLevel()/2+ "%");
    }
    
    public void accelerationInputReceived(WRAccelerationEvent evt)
    {
        if (accelerometerSource)
        {
            remoteX = (int)(evt.getXAcceleration()*300)+300;
            remoteY = (int)(evt.getYAcceleration()*300)+300;
            
            remotePanel.setXY(remoteX,remoteY);
            remotePanel.increaseT();
            remotePanel.repaint();
        }
        
        StoredData.addCoordinate(new Coordinate(remotePanel.getID(), remoteX, remoteY));
    }
    
    public void extensionInputReceived(WRExtensionEvent evt)
    {
        if (evt instanceof WRNunchukExtensionEvent)
        {
            WRNunchukExtensionEvent NEvt = (WRNunchukExtensionEvent)evt;
            WRAccelerationEvent AEvt = NEvt.getAcceleration();
            nunchukX = (int)(AEvt.getXAcceleration()*300)+100;
            nunchukY = (int)(AEvt.getYAcceleration()*300)+200;
            
            nunchukPanel.setXY(nunchukX,nunchukY);
            nunchukPanel.increaseT();
            nunchukPanel.repaint();
            StoredData.addCoordinate(new Coordinate(nunchukPanel.getID(), nunchukX, nunchukY));
        }
    }
    
    public void extensionConnected(WiiRemoteExtension extension)
    {
        System.out.println("Extension connected!");
        try
        {
            remote.setExtensionEnabled(true);
        }catch(Exception e){e.printStackTrace();}
    }
    
    public void extensionPartiallyInserted()
    {
        System.out.println("Extension partially inserted. Push it in more next time!");
    }
    
    public void extensionUnknown()
    {
        System.out.println("Extension unknown. Did you try to plug in a toaster or something?");
    }
    
    public void extensionDisconnected(WiiRemoteExtension extension)
    {
        System.out.println("Extension disconnected. Why'd you unplug it, eh?");
    }
}



/*-Dbluecove.jsr82.psm_minimum_off=true
-Dbluecove.debug=false*/